package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 * http://itbbs.pconline.com.cn/ 太平洋电脑论坛
 *
 * @author Persh
 */
public class PconlineBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = { 
				"http://itbbs.pconline.com.cn/.*/[0-9]*.html",
				"http://softbbs.pconline.com.cn/[0-9]*.html",
				"http://cafe.pconline.com.cn/[0-9]*.html",
				"http://bbs.pcauto.com.cn/topic-[0-9]*.html",
				"http://piebbs.pconline.com.cn/topic-[0-9]*.html",
				"http://bbs.pchouse.com.cn/topic-[0-9]*.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	@SuppressWarnings("deprecation")
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Integer clickNum = null;
		Integer commentNum = null;

		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("title").text();
		try {
			title = title.substring(0, title.indexOf("_"));
		} catch (Exception e1) {
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("strong.titArea").text();
		}
		Element firstDiv = doc.select("div#post_list > div[id^=post_]").first();
		if (firstDiv!=null){
			author = firstDiv.select("dt.fb a").text();
			if(StringUtils.isBlank(author)){
				author = firstDiv.select("p.uName").text();
			}
			date = DateUtils.matchDate(firstDiv.select("div.post_info").text());

			Elements element = firstDiv.select("div.post_msg");
			content = HtmlCleaner.getContentHtml(url,  element);

		}
		if(StringUtils.isBlank(author)){
			Elements tmpauthor = doc.select("a.username");
			if(tmpauthor.size()>0){
				author = tmpauthor.first().text();
			}
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url, doc.select("div.topiccontent"));
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("span.date").text());
		}

		String commTxt = doc.select("thead.thead tr th.tSide").text();
		clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "回复"));
		commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));

		if(clickNum == null && commentNum==null ){
			commTxt = doc.select("p.overView").text();
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "回复"));
			commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
		}

		if (StringUtils.isBlank(title)) {
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setAuthor(author);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);

		/**
		 * 更新路径
		 */
		String updateUrl = url;
		bbs.setUpdateUrl(updateUrl);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String htmltxt = responseToURL.getHtml();
					Document doc = Jsoup.parse(htmltxt);
					Integer clickNum = null;
					Integer commentNum = null;
					String commTxt = doc.select("thead.thead tr th.tSide").text();
					clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "回复"));
					commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
					if(clickNum == null && commentNum==null ){
						commTxt = doc.select("p.overView").text();
						clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "回复"));
						commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}

	
	
}
